/*
 * @Author: yangni 920556271@qq.com
 * @Date: 2024-03-12 10:02:58
 * @LastEditors: yangni 920556271@qq.com
 * @LastEditTime: 2024-05-13 10:02:09
 * @FilePath: /yt-devops/lib/pages/devices/charge_termimal_list/charge_termimal_list_view.dart
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import 'package:bruno/bruno.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:yt_devops/components/app_bar/app_bar_widget.dart';
import 'package:yt_devops/components/gaps.dart';
import 'package:yt_devops/components/text_width.dart';
import 'package:yt_devops/pages/devices/device_controll/page_component/charge_terminal/charge_termimal_list/charge_termimal_list_model.dart';
import 'package:yt_devops/resources/resources.dart';
import 'package:yt_devops/resources/str_res_keys.dart';
import 'package:yt_devops/styles/styles.dart';
import 'package:yt_devops/utils/enums.dart';
import 'package:yt_devops/utils/func_utils.dart';
import 'charge_termimal_list_controller.dart';

class ChargeTermimalListPage extends GetView<ChargeTermimalListController> {
  final int deviceId;
  ChargeTermimalListPage(this.deviceId, {Key? key}) : super(key: key);

  @override
  final ChargeTermimalListController controller =
      Get.put(ChargeTermimalListController());

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBarWidget.withRightActions(ResKey.deviceSplitChargingPile.tr,
            actions: [
              GestureDetector(
                onTap: controller.addChargeEnd,
                child: Padding(
                  padding: EdgeInsets.fromLTRB(10.w, 2.h, 20.w, 2.h),
                  child: const Icon(
                    Icons.add,
                  ),
                ),
              )
            ]),
        body: GetBuilder<ChargeTermimalListController>(
          initState: (state) {
            controller.initData(deviceId);
          },
          builder: (controller) => ListView.builder(
            padding: EdgeInsets.only(bottom: 16.h),
            itemBuilder: (context, index) => itemWidget(
              controller.terminalList[index],
            ),
            itemCount: controller.terminalList.length,
          ),
        ));
  }

  Widget itemWidget(ChargeTerminalModel model) {
    return Container(
      padding: EdgeInsets.fromLTRB(16.w, 0, 16.w, 0),
      margin: EdgeInsets.fromLTRB(16.w, 10, 16.w, 0),
      decoration: BoxDecoration(
          color: Colour.white, borderRadius: BorderRadius.circular(10)),
      child: Column(crossAxisAlignment: CrossAxisAlignment.start, children: [
        SizedBox(
          height: 53,
          child: Row(
            children: [
              Expanded(
                child: Text.rich(
                  TextSpan(text: model.deviceName),
                  style: TextStyle(
                      color: Colour.mainTextColor,
                      fontSize: 18.sp,
                      fontWeight: FontWeight.w500),
                  overflow: TextOverflow.ellipsis,
                ),
              ),
              Padding(
                padding: EdgeInsets.only(left: 10.w),
                child: statusWidget(model.status),
              ),
              operateWidget(model)
            ],
          ),
        ),
        infoWidget(ResKey.siteName.tr, model.siteName),
        infoWidget(ResKey.deviceSn.tr, model.sn),
        infoWidget(ResKey.alarmStatus.tr, model.alarmStatus.label,
            valueTextStyle: alarmTextStyle(model.alarmStatus)),
        infoWidget(ResKey.productType.tr, model.productTypeName),
        infoWidget(ResKey.productModel.tr, model.productName),
        const SizedBox(
          height: 16,
        ),
        const Divider(
          color: Colour.dividerColor,
          height: 1,
        ),
        bindMainServer(model)
      ]),
    );
  }

  Widget statusWidget(ConnectStatusType statusType) {
    Color color;
    switch (statusType) {
      case ConnectStatusType.on:
        color = const Color(0xFF00AE66);
        break;

      default:
        color = Colour.labelTextColor;
        break;
    }
    return Container(
      alignment: Alignment.center,
      padding: EdgeInsets.symmetric(horizontal: 4.w),
      height: 20.h,
      color: color.withOpacity(0.1),
      child: Text(
        statusType?.label ?? '',
        style: TextStyles.styleWith(12.sp, color: color),
      ),
    );
  }

  Widget infoWidget(String label, String value, {TextStyle? valueTextStyle}) {
    return Padding(
      padding: const EdgeInsets.only(bottom: 8),
      child: Row(
        children: [
          SizedBox(
            width: 105.w,
            child: Text(label, style: TextStyles.lf14),
          ),
          Expanded(
            child: Text(
              value,
              style: valueTextStyle ??
                  TextStyle(
                    color: Colour.mainTextColor,
                    fontSize: 14.sp,
                  ),
              overflow: TextOverflow.ellipsis,
            ),
          )
        ],
      ),
    );
  }

  Widget operateWidget(ChargeTerminalModel data) {
    return Row(
      children: [
        Gaps.hGap12,
        GestureDetector(
          onTap: () => controller.toTel(data),
          child: Image.asset(ImageAssets.iconDeviceEdit),
        ),
        Gaps.hGap10,
        GestureDetector(
          onTap: () => controller.deleteDeviceHandle(data.id),
          child: Image.asset(ImageAssets.iconDelete),
        ),
      ],
    );
  }

  /// 关联主机
  Widget bindMainServer(ChargeTerminalModel model) {
    return SizedBox(
      height: 48,
      child: Row(mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [
        Text(
          ResKey.deviceAssociatedHosts.tr,
          style: TextStyles.f14,
        ),
        BrnSwitchButton(
            value: model.isBindMainServer == 0,
            onChanged: (bool val) {
              controller.bindMainService(model.id, val);
            })
      ]),
    );
  }

  TextStyle alarmTextStyle(DeviceAlarmStatus alarm) {
    Color color = alarm == DeviceAlarmStatus.normal
        ? Colour.green
        : Colour.warningTextColor;
    return TextStyles.f14.copyWith(color: color);
  }
}
